package com.atm.bulksms;



import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import android.app.ExpandableListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import com.atm.bulksms.data.BulkSMSData.GroupData;


public class GroupList extends ExpandableListActivity {

    ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       Cursor cursor =  getContentResolver().query(GroupData.CONTENT_URI, null, null, null, null);
        Hashtable<String, Vector<String>> tree = new Hashtable<String, Vector<String>>();
        
        cursor.moveToFirst();
        while(cursor.moveToNext()){
        	String groupName = cursor.getString(cursor.getColumnIndex(GroupData.GROUP_NAME));
        	String phone = cursor.getString(cursor.getColumnIndex(GroupData.PHONE_NUMBER));
        	Vector<String> nos = tree.get(groupName);
        	if(nos == null){ // new entry
        		nos = new Vector<String>();
        	}
        	nos.add(phone);
        	tree.put(groupName, nos);
        }
        
        mAdapter = new MyExpandableListAdapter(tree);
        setListAdapter(mAdapter);
       // registerForContextMenu(getExpandableListView());
    }

   

  
    /**
     * A simple adapter which maintains an ArrayList of photo resource Ids. 
     * Each photo is displayed as an image. This adapter supports clearing the
     * list of photos and adding a new photo.
     *
     */
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    	
    	public MyExpandableListAdapter(Hashtable<String, Vector<String>> tree) {
    		groups = new String[tree.size()];
    		children = new String[tree.size()][];
    		Enumeration<String> list = tree.keys();
    		int index = 0;
    		while(list.hasMoreElements()){
    			String name = list.nextElement();
    			groups[index] = name;
    			Vector<String> v = tree.get(name);
    			String[] arr = new String[v.size()] ;
    			v.copyInto(arr);
    			children[index++] = arr;
    		}
		}
        // Sample data set.  children[i] contains the children (String[]) for groups[i].
        private String[] groups = null;//{ "People Names", "Dog Names", "Cat Names", "Fish Names" };
        private String[][] children = null; /*{
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" },
                { "Goldy", "Bubbles" }
        };*/

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(GroupList.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public boolean hasStableIds() {
            return true;
        }

    }
}

